#include "image.h"
using namespace EPOC;

/*****************************************
  Image::Image 
 *****************************************/
Image::Image()
{
	SetClassID(kCLASSID_IMAGE);

	SetColorkeyEnabled(false);
	SetAlphaEnabled(false);
	SetFramesEnabled(false);
	
	SetFrames(1, 1, 0);
	SetCurrentFrame(0);
}


/*****************************************
  Image::~Image
 *****************************************/
Image::~Image()
{
	Image::Cleanup();
}


/*****************************************
  Image::Cleanup
 *****************************************/
void Image::Cleanup()
{
}


/*****************************************
  Image::Initialize(
 *****************************************/
bool Image::Initialize(Texture *pTexture/*, RENDER_MODE nRenderMode*/)
{
	if(GraphicsObject::Initialize(pTexture/*, nRenderMode*/) == false)
		return false;
	
	return true;
}

/*****************************************
  Image::RenderSDL
 *****************************************/
void Image::RenderSDL(TextureSDL *pDestTexture)
{
	if( !GetTexture())
		return;

	if(IsFramesEnabled())
	{
		TextureSDL *pTexture = (TextureSDL*) GetTexture();

		if(IsAlphaEnabled())
			SDL_SetSurfaceAlphaMod(pTexture->GetSDLSurface(), m_nAlpha);
		else
			SDL_SetSurfaceAlphaMod(pTexture->GetSDLSurface(), SDL_ALPHA_OPAQUE);

		if(IsColorkeyEnabled())
			SDL_SetColorKey(pTexture->GetSDLSurface(), SDL_TRUE  | SDL_RLEACCEL, SDL_MapRGB(pTexture->GetSDLSurface()->format, m_rColorKey.m_nRed, m_rColorKey.m_nGreen, m_rColorKey.m_nBlue));
		else
			SDL_SetColorKey(pTexture->GetSDLSurface(), 0, 0);

		int nFramesX = m_nCurrentFrame % m_nFramesX;
		int nFramesY = m_nCurrentFrame / m_nFramesX;

		float fFrameWidth = (float) pTexture->GetDimension()->m_nWidth / (float) m_nFramesX;
		float fFrameHeight = (float) pTexture->GetDimension()->m_nHeight / (float) m_nFramesY;

		SDL_Rect rSource;
		rSource.x = (Sint16)(fFrameWidth * nFramesX + 0.5f);
		rSource.y = (Sint16)(fFrameHeight * nFramesY + 0.5f);
		rSource.w = (Uint16)(fFrameWidth + 0.5f);
		rSource.h = (Uint16)(fFrameHeight + 0.5f);

		SDL_Rect rDest;

		if(IsPivotCentered())
		{
			rDest.x = (Sint16)(GetPos()->m_fX - (fFrameWidth * 0.5f) + 0.5f);
			rDest.y = (Sint16)(GetPos()->m_fY - (fFrameHeight * 0.5f) + 0.5f);
		}
		else
		{
			rDest.x = (Sint16)(GetPos()->m_fX + 0.5f);
			rDest.y = (Sint16)(GetPos()->m_fY + 0.5f);
		}

		rDest.w = (Uint16)(fFrameWidth + 0.5f);
		rDest.h = (Uint16)(fFrameHeight + 0.5f);

		SDL_BlitSurface(pTexture->GetSDLSurface(), &rSource, pDestTexture->GetSDLSurface(), &rDest);
	}
	else
	{
		GraphicsObject::RenderSDL(pDestTexture);
	}
}


/*****************************************
  Image::RenderOGL
 *****************************************/
void Image::RenderOGL()
{
	//TODO: Add frame support for OGL textures

	if( !GetTexture())
		return;

	if(IsFramesEnabled())
	{
		int nFrameX = m_nCurrentFrame % m_nFramesX;
		int nFrameY = m_nCurrentFrame / m_nFramesX;

		float fFrameWidth = 1.0f / (float) m_nFramesX;
		float fFrameHeight = 1.0f / (float) m_nFramesY;
		
		GetUVData()->m_fUStart = fFrameWidth * nFrameX;
		GetUVData()->m_fVStart = fFrameHeight * nFrameY;
		GetUVData()->m_fUEnd = GetUVData()->m_fUStart + fFrameWidth;
		GetUVData()->m_fVEnd = GetUVData()->m_fVStart + fFrameHeight;
	}
	
	GraphicsObject::RenderOGL();
}